<?php

include_once 'autoload.php';

class FormLogin extends Zend_Form {

    public function init() {

        $this->setName('Login');

        $userName = new Zend_Form_Element_Text('userName');

        $userName->setLabel('Nom: ')
                ->setRequired();

        $password = new Zend_Form_Element_Password('password');

        $password->setLabel('Mot de passe:')
                ->setRequired();

        $login = new Zend_Form_Element_Submit('login');

        $login->setLabel('Envoyer');

        $this->addElements(array($userName, $password, $login))
                ->setMethod('post')
                ->setAction('formAuth01.php');
    }

}

function estAutentifier($userName, $userPwd) {
    $db = new Zend_Db_Adapter_Pdo_Mysql(array('host' => 'localhost', 'username' => 'root', 'password' => '', 'dbname' => 'gsb'));
    $authAdapter = new Zend_Auth_Adapter_DbTable($db);
    $authAdapter->setTableName('visiteur')
            ->setIdentityColumn('login')
            ->setCredentialColumn('mdp');
    $authAdapter->setIdentity($userName)
            ->setCredential($userPwd);
    // ->setCredentialTreatment('MD5(?)');
    $auth = Zend_Auth::getInstance();
    $result = $auth->authenticate($authAdapter);
    return($result->isValid() ? TRUE : FALSE);
}

$view = new Zend_View(); //autoload

$form = new FormLogin(); //autoload

if (!$_POST) {

// rendu du formulaire vierge 

    echo $form->render($view);
} else

if (!$form->isValid($_POST)) {

// rendu du formulaire rempli et messages d'erreurs 

    echo $form->render($view);
} else {

// formulaire valide.traiter les donnees postees 
//Zend_Debug::dump($_POST); idem var_dump($_POST) 

    Zend_Debug::dump($form->getValues());
    $userName = $form->getValue('userName');
    $userPwd = $form->getValue('password');
    if (estAutentifier($userName, $userPwd)) {
        echo 'parfait';
    } else {
        echo'recommence';
    }
}
?>